// pages/myIncome/myIncome.js
const app = getApp()
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    limit: 10,
    total_page:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.totalMoney()
    this.incomeDetail()
  },

  // 收入累计
  totalMoney: function () {
    var that = this;
    api.POST({
      url: app.globalData.base_url + 'coupons/incomeStatistics',
      params: {},
      success: function (res) {
        if (res.data.code == '') {
          that.setData({
            totalMoney: res.data.data,
          })
        }
      },
    })
  },

  // 收入明细
  incomeDetail: function () {
    var that = this;
    api.POST({
      params: {
        page: that.data.page,
        limit: that.data.limit,
      },
      url: app.globalData.base_url + 'coupons/incomeDetail',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          var paging = res.data.data.paging;
          var currentPage = paging.page;
          var totalPage = paging.total_page;
          var list = res.data.data.list;

          if (that.data.page == 1) {
            var itemData = list;
          } else {
            var itemData = that.data.incomeDetail.concat(list)
          }
          that.setData({
            hasMore: (currentPage >= totalPage) ? false : true,
            total_page: totalPage,
            incomeDetail: itemData
          })
        }
      },
      fail: function (fail) {
        util.showError("网络错误");
      }
    })
  },

  //加载更多
  loadMore: function () {
    if (this.data.page >= this.data.total_page) {
      return;
    }
    this.setData({
      page: this.data.page + 1
    });
    this.incomeDetail();
  },

  //刷新页面
  resetData: function () {
    var that = this
    that.setData({
      page: 1,
      hasMore: true
    });
    this.totalMoney();
    this.incomeDetail();
  },
  //页面下拉刷新
  onPullDownRefresh: function () {
    var that = this;

    setTimeout(function () {
      that.resetData()
    }, 1000)
  },

  //页面上拉触底
  onReachBottom: function (e) {
    this.loadMore();
  },
})